package com.zige.zige.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;

import com.zige.zige.LittleZebraApplication;
import com.zige.zige.R;
import com.zige.zige.activity.SettingCenterActivity;
import com.zige.zige.constants.UrlConsts;
import com.zige.zige.task.PostRequestTask;
import com.zige.zige.task.TaskExecutor;
import com.zige.zige.utils.SharedPreferencesUtils;
import com.zige.zige.utils.SystemUtils;

import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by fei on 2015/4/24.
 */
public class ExitDialog extends DialogFragment {

    private LittleZebraApplication application;
    private SettingCenterActivity activity;
    int code;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        application= (LittleZebraApplication)LittleZebraApplication.getInstance();
        activity = (SettingCenterActivity) getActivity();
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
        builder.setMessage(R.string.exit_message);
        builder.setPositiveButton(R.string.btn_ok,new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dismiss();
               logout();

            }
        });
        builder.setNegativeButton(R.string.btn_cancle,new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dismiss();
            }
        });
        return builder.create();
    }

    private void  logout(){

        String deviceId = SystemUtils.getDeviceKey(getActivity());
        String uid = application.getUserInfo().userID;

        HashMap<String,String> params = new HashMap<>();
        params.put("deviceId",deviceId);
        params.put("uid", uid);
        activity.showLoadingDialog();
        TaskExecutor.getInstance().execute(new PostRequestTask(params,getActivity(), UrlConsts.USER_LOGOUT) {
            @Override
            protected void handleResponse(String response) {
            try {
                JSONObject  object = new JSONObject(response);
               int code = object.getInt("code");
                if(code==0000){
                    activity.closeLoadingDialog();
                    application.setUserInfo(null);
                    SharedPreferencesUtils.saveValue(activity, SharedPreferencesUtils.keep_login_state, false);
                    SharedPreferencesUtils.saveValue(getActivity()
                            , SharedPreferencesUtils.user_safetyLockPassword, false);
                    dismiss();
//                    getActivity().startActivity(new Intent(getActivity(), MainActivity.class));
                    getActivity().finish();
                    activity.finish();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            }

            @Override
            protected void handleError() {
                super.handleError();
                activity.closeLoadingDialog();
//                getActivity().startActivity(new Intent(getActivity(), MainActivity.class));
                getActivity().finish();
                activity.finish();
            }
        });

    }


}
